<template>
  <div class="container">
    <div class="registerForm">
      
      <img src="../assets/logo_1.png" class="logo" @click="backIndex"/>
      <div class="choice1" ref="choice1">
        <p @click="changeTab1(false)">学生登录</p>
        <p @click="changeTab1(true)">企业登录</p>
      </div>
      <el-form :model="loginInfo"  status-icon :rules="rules2" ref="loginInfo" label-width="100px" class="loginForm" >
        <div v-if="!isHR">
        <el-form-item  prop="username">
          <el-input type="text" v-model="loginInfo.account" auto-complete="off" class="loginInput" placeholder="请输入用户账号"></el-input>
        </el-form-item>
        <el-form-item  prop="password">
          <el-input type="password" v-model="loginInfo.password" auto-complete="off" class="loginInput" placeholder="请输入密码"></el-input>
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" class="loginBtn" @click="submitForm('loginInfo')" >登录</el-button>

        </el-form-item>
        </div>
      </el-form>

      <el-form :model="hrloginInfo" :rules="rules1" status-icon  ref="loginInfo" label-width="100px" class="loginForm" >
        <div v-if="isHR">
        <el-form-item  prop="username">
          <el-input type="text" v-model="hrloginInfo.name" auto-complete="off" class="loginInput" placeholder="请输入企业名"></el-input>
        </el-form-item>
        
        <el-form-item  prop="password">
          <el-input type="password" v-model="hrloginInfo.upassword" auto-complete="off" class="loginInput" placeholder="请输入密码"></el-input>
        </el-form-item>
        <br>
        <el-form-item>
          <el-button type="primary" class="loginBtn" @click="hrsubmitForm('hrloginInfo')" >登录</el-button>

        </el-form-item>
        </div>
      </el-form>
      
       
      
      <div class="footer-tip" @click="toRegister">
       没有账号？直接注册
      </div>
      <div class="footer-tip99" @click="backIndex">
       返回首页
      </div>
      
    </div>
   <!-- <img class="bg_bottom" src="../assets/bg_bottom.png"/>
  <img class="bg_bottom2" src="../assets/bg_bottom2.png"/> -->
  </div>
</template>

<style>
@import "../assets/Animate/animate.min.css";

  body {
    padding: 0;
    margin: 0;
  }

  * {
    box-sizing: border-box;
  }

  .container {
    width: 100%;
    position: relative;
    height: 100%;
    overflow: hidden;
  }

  .container::before {
    content: '';
    position: absolute;
    /* background-image: url("https://upload-images.jianshu.io/upload_images/9381131-261e4e2e3fca50f5.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240"); */
    background-size: 100% 100%;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    transform: matrix(1, 0, 0, 1, 0, 0);
    transition: all 500ms linear 0s;
  }

  .container:hover::before {
    transform: matrix(1.05, 0, 0, 1.05, 3.07, 5.7)
  }
  .choice1 {
    text-align: left;
    padding-left: 80px;
  }

  .choice1 p{
    display: inline-block;
    width: 173px;
    height: 30px;
    text-align: center;
    line-height: 30px;
    color: #5a5a5a;
  }

  .choice1 p:first-child {
    border-right: 1px solid #ededed;
    font-weight: 700;
  }

  .registerForm {
    background: #fff;
    border: 1px solid #ededed;
    width: 500px;
    height: 400px;
    margin: 120px auto 150px auto;
    box-shadow: 0px 5px 8px #888;
    border-radius: 8px;
    position: relative;
  }
 
  

  .loginForm {
    padding: 0 36px;
  }

  .el-form-item__content {
    margin-left: 0px !important;
  }

  .loginInput {
    width: 100%;
  }
  
  
  .footer-tip {
    color: rgba(0, 0, 0, 0.5);
    font-size: 16px;
    cursor: pointer;
    position: absolute;
    bottom: 16px;
    right: 16px;
  }
  .footer-tip99 {
    color: rgba(0, 0, 0, 0.5);
    font-size: 16px;
    cursor: pointer;
    position: absolute;
    bottom: 16px;
    left: 16px;
  }

  .bg_bottom {
    position: fixed;
    bottom: 0;
    right: 0;
  }

  .bg_bottom2 {
    position: fixed;
    bottom: 0;
    left: 0;
  }

  .logo {
    width: 150px;
    height: 80px;;
    cursor: pointer;
    opacity: 0.7;
  }

  .loginBtn {
    width: 100%;
  }

</style>

<script>/* eslint-disable indent */

  import fetch from '../api/fetch.js'

  export default {
    data () {
      var validUsername = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入用户名'))
        } else {
          callback()
        }
      }
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'))
        } else {
          callback()
        }
      }
      var validmail = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入邮箱'))
        } else {
          callback()
        }
      }
      return {
        isHR:false,
        loginInfo: {
          account: '',
          password: ''
          
        },
        hrloginInfo: {
          name: '',
          upassword: ''
          
        },
        rules2: {
          account: [{validator: validUsername, trigger: 'blur'}],
          password: [{validator: validatePass, trigger: 'blur'}]
        },
        rules1: {
          name: [{validator: validUsername, trigger: 'blur'}],
          upassword: [{validator: validatePass, trigger: 'blur'}]
        }
      }
    },
    mounted() {
        this.addAnimation()
    },
    methods: {
      addAnimation() {
        let form = document.getElementsByClassName('registerForm')[0];
        form.classList.add('animated')
        form.classList.add('bounceInDown')
      },
      backIndex () {
        this.$router.push({name: 'index'})
      },
      toRegister () {
        this.$router.push({name: 'register'})
      },
      tofindpass () {
        this.$router.push({name: 'findpass'})
      },
      // 登录切换角色
      
      // submitForm (formName) {
      //   this.$refs[formName].validate(valid => {
      //     if (valid) {
      //       fetch
      //         .userLogin(this.loginInfo)
      //         .then(res => {
      //           if (res.status === 200) {
      //             if (res.data.success === true) {
      //               localStorage.setItem('token', res.data.data.token)
      //               localStorage.setItem('companyId', res.data.data.companyId)
      //               localStorage.setItem('role', res.data.data.role)
      //               sessionStorage.setItem('userId', res.data.data.userId)
      //               if (res.data.data.role === 2) {
      //                 this.$router.push({name:'index'})
      //               } else {
      //                 this.$router.push({name: 'hrView', params: {hrRefresh: 2}})
      //               }
                  // } else {
                  //   this.$message({
                  //     message: '用户名或密码错误',
                  //     type: 'warning'
                  //   })
                  // }
      //           }
      //         })
      //         .catch(e => {
      //           console.log(e)
      //         })
      //     }
      //   })
      // }
      submitForm (formName) {
        this.$refs[formName].validate(valid => {
          if (valid) {
           
            fetch
              .userLogin(this.loginInfo)
              .then(res => {
                console.log(res)

                if(res.data.code===200){
                  
                     localStorage.setItem('token',true)
                    
                    sessionStorage.setItem('role', 2)
                    sessionStorage.setItem('userId', res.data.msg)
                    
                    this.$router.push({name:'index'})
                    
                }else {
                    this.$message({
                      message: '用户名或密码错误',
                      type: 'warning'
                    })
                  }
                  
                
              })
              .catch(e => {
                console.log(e)
              })
          }
        })
      },
      hrsubmitForm (formName) {
        
          
            fetch
              .hrLogin(this.hrloginInfo)
              .then(res => {
                console.log(res)
                if(res.data.code==200){
                  
                     localStorage.setItem('token', true)
                    // localStorage.setItem('companyId', res.data.data.companyId)
                    sessionStorage.setItem('role', 1)
                    sessionStorage.setItem('userId', res.data.msg)
                    
                      this.$router.push({name:'index'})
                    
                  
                }else {
                    this.$message({
                      message: '用户名或密码错误',
                      type: 'warning'
                    })
                  }
                
              })
              .catch(e => {
                console.log(e)
              })
          
        
      },

      changeTab1(isHR) {
      if (!isHR) {
        this.$refs.choice1.firstChild.style.fontWeight = '700'
         this.$refs.choice1.lastChild.style.fontWeight = '400'
      } else {
        this.$refs.choice1.firstChild.style.fontWeight = '400'
        this.$refs.choice1.lastChild.style.fontWeight = '700'
      }
      this.isHR = isHR;
    },

      // submitForm(loginInfo){
      //   // var url="106.53.200.72:8080/auth/student/login"
      //   var params={
      //     account:loginInfo.account,
      //     password:loginInfo.password
      //   };
      //   fetch
      //       .userLogin(params)
      //       .then(res=>{
      //         if(res.data!=' '){
      //         alert("成功");
      //           this.$router.push({name:'index'})
      //         }
      //       }

      //       )

      //   // $.ajax({
      //   //   "url":url,
      //   //   "data":params,
      //   //   "type":"post",
      //   //   "dataType":"json",
          

      //   // })
      // }

      
    }
  }
</script>
